package modellayer;
import java.util.ArrayList;
import java.io.*;

/**
 * Write a description of class AnsatCont here.
 * 
 * @author (Kasper) 
 * @version (05.12.2014)
 */
public class AnsatCont
{
    private ArrayList<Ansat> ansatte;
    private static AnsatCont instance;
    private PersonFileHandler fileHandler;

    /**
     * Singleton Constructor for objects of class AnsatCont
     */
    public AnsatCont()
    {
        ansatte = new ArrayList<Ansat>();
        fileHandler = new PersonFileHandler("ansatData");
        Leder admin = new Leder("admin", "admin", "admin", "admin", "admin", "admin");
        admin.setLederLogin("admin");
        admin.setPassword("admin");
        ansatte.add(admin);
    }

    /**
     * Singleton Method getInstance
     * 
     * Creates a AnsatCont if one does not exist
     *
     * @return instance
     */
    public static AnsatCont getInstance()
    {
        if(instance == null)
        {
            instance = new AnsatCont();
        }
        return instance;
    }

    /**
     * Method getList
     *
     * @return A list with employees
     */
    public ArrayList<Ansat> getList()
    {
        return ansatte;
    }

    /**
     * Method addAnsat
     * 
     * adds an employee to the arraylist of employees
     *
     * @param a An employee
     */
    public void addAnsat(Ansat a)
    {
        ansatte.add(a);
    }

    /**
     * Method findAnsat
     *
     * Searches for an employee with the given phone number
     * 
     * @param tlf Telephone number
     * @return An employee of the type Ansat
     */
    public Ansat findAnsat(int medarbejderNr)
    {
        Ansat returnAnsat = null;
        boolean found = false;
        int size = ansatte.size();
        for(int i = 0; i < size && !found; i++)
        {
            if(ansatte.get(i).getMedarbejderNr() == medarbejderNr)
            {
                returnAnsat = ansatte.get(i);
                found = true;
            }
        }
        return returnAnsat;
    }

    /**
     * Method findAnsatViaTelefon
     *
     * Searches for an employee via phone number
     * 
     * @param tlf Telephone number
     * @return An employee
     */
    public Ansat findAnsatViaTelefon(String tlf)
    {
        Ansat returnAnsat = null;
        boolean found = false;
        int size = ansatte.size();
        for(int i = 0; i < size && !found; i++)
        {
            if(ansatte.get(i).getTlf().equals(tlf))
            {
                returnAnsat = ansatte.get(i);
                found = true;
            }
        }
        return returnAnsat;
    }

    /**
     * Method deleteAnsat
     *
     * Removes an employee with the given phone number. Returns a boolean on wether or not
     * the person was removed.
     * 
     * @param tlf Telephone number
     * @return False if the employee couldn't be found. True if the employee was removed.
     */
    public boolean deleteAnsat(int medarbejderNr)
    {
        boolean success = true;
        if(findAnsat(medarbejderNr) == null)
        {
            success = false;
        }
        Ansat tempA = findAnsat(medarbejderNr);
        ansatte.remove(tempA);

        return success;
    }

    /**
     * Method updateAnsat
     *
     *
     * Updates all given parameters of an employee
     * 
     * @param oldTlf Old telephonenumber
     * @param navn Name
     * @param adresse Adress
     * @param postnr Postnumber
     * @param by City
     * @param tlf Telephonenumber
     * @param email E-mail
     * @param login Loginname
     * @param password 
     * @return True if the update succeded. False if not. 
     */
    public boolean updateAnsat(int medarbejderNr, int newMedarbejderNr, String navn, String adresse, String postnr, String by, String tlf, String email, String password)
    {
        boolean success = false;
        Ansat tempA = findAnsat(medarbejderNr);
        tempA.setMedarbejderNr(newMedarbejderNr);
        tempA.setNavn(navn);
        tempA.setAdresse(adresse);
        tempA.setPostnr(postnr);
        tempA.setBy(by);
        tempA.setTlf(tlf);
        tempA.setEmail(email);
        tempA.setPassword(password);
        if(tempA instanceof Medarbejder)
        {
            Medarbejder medarbejder = (Medarbejder) tempA;
            if(tempA.getNavn().equals(navn) && tempA.getAdresse().equals(adresse) && tempA.getPostnr().equals(postnr) && tempA.getBy().equals(by) && tempA.getTlf().equals(tlf) && tempA.getEmail().equals(email) && tempA.getPassword().equals(password))
            {
                success = true;
            }
        }

        if(tempA instanceof Leder)
        {
            Leder leder = (Leder) tempA;
            if(tempA.getNavn().equals(navn) && tempA.getAdresse().equals(adresse) && tempA.getPostnr().equals(postnr) && tempA.getBy().equals(by) && tempA.getTlf().equals(tlf) && tempA.getEmail().equals(email) && tempA.getPassword().equals(password))
            {
                success = true;
            }
        }

        return success;
    }

    public void saveToFile() throws IOException
    {
        fileHandler.saveToFileAnsat(ansatte);
    }

    public void loadFromFile()
    throws IOException, ClassNotFoundException
    {
        ansatte = fileHandler.readFromFileAnsat();
    }

    /**
     * Method printInfo
     *
     * Prints info about employees.
     */
    public void printInfo()
    {
        for(int i = 0; i < ansatte.size(); i++)
        {
            System.out.println(ansatte.get(i));
        }
    }
}